# coding=utf-8

from django.db.models import Model, CharField, GenericIPAddressField, IntegerField, TextField, DateTimeField, \
    ManyToManyField, ForeignKey, DO_NOTHING, BooleanField, AutoField, FloatField
import datetime


class MagicUser(Model):
    """
    魔数用户表
    """
    class Meta:
        db_table = 'magic_user'

    id = IntegerField(auto_created=True, primary_key=True)
    username = CharField(max_length=255, blank=True, null=True)
    password = CharField(max_length=255, blank=True, null=True)
    is_deleted = IntegerField(default=0)
    create_at = DateTimeField(auto_now_add=True, blank=True, null=True)
    modify_at = DateTimeField(auto_now=True, blank=True, null=True)
    user_permission = CharField(max_length=512, blank=True, null=True)


class MagicMockProfit(Model):
    class Meta:
        db_table = 'magic_mock_profit'

    id = AutoField(auto_created=True, primary_key=True)
    adv_id = CharField(max_length=32, null=True, blank=True)
    adv_title = CharField(max_length=255, null=True, blank=True)
    adv_views = IntegerField(blank=True)
    adv_clicks = IntegerField(blank=True)
    adv_eCPM = IntegerField(blank=True)
    adv_profit = FloatField(blank=True)
    adv_date = CharField(max_length=12, null=True, blank=True)
    is_deleted = IntegerField(default=0, blank=True)
    create_at = DateTimeField(blank=True, null=True)
    modify_at = DateTimeField(blank=True, null=True)

    def to_dict(self):
        opts = self._meta
        data = {}
        for f in opts.concrete_fields:
            value = f.value_from_object(self)
            if isinstance(value, datetime):
                value = value.strftime('%Y-%m-%d %H:%M:%S')
            data[f.name] = value
        return data
